﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using YAPL.Language.Core.Exceptions;

namespace YAPL.Language.Core.Types {
	public class YAPLFloat : YAPLObject {
		private double _floatValue;
		public YAPLFloat(double floatValue)
			: base("YAPLFloat") {
			_floatValue = floatValue;
		}

		public double FloatValue {
			get { return _floatValue; }
			set { _floatValue = value; }
		}

		public override string ToString() {
			return FloatValue.ToString(new NumberFormatInfo());
		}

		public override object Value {
			get {
				return _floatValue;
			}
		}
		#region operator overrides

		public override YAPLObject Add(YAPLObject other) {
			if (other is YAPLFloat) {
				var otherValue = (other as YAPLFloat).FloatValue;
				return new YAPLFloat(FloatValue + otherValue);
			}
			if (other is YAPLInt) {
				var otherValue = (other as YAPLInt).IntValue;
				return new YAPLFloat(FloatValue + otherValue);
			}
			if (other is YAPLString) {
				var strValue = (other as YAPLString).StrValue;
				return new YAPLString(FloatValue + strValue);
			}
			return base.Add(other);
		}

		public override YAPLObject Subtract(YAPLObject other) {
			if (other is YAPLFloat) {
				var otherValue = (other as YAPLFloat).FloatValue;
				return new YAPLFloat(FloatValue - otherValue);
			}
			if (other is YAPLInt) {
				var otherValue = (other as YAPLInt).IntValue;
				return new YAPLFloat(FloatValue - otherValue);
			}
			return base.Add(other);
		}

		public override YAPLObject Multiply(YAPLObject other) {
			if (other is YAPLInt) {
				var otherValue = (other as YAPLInt).IntValue;
				return new YAPLFloat(FloatValue * otherValue);
			}
			if (other is YAPLFloat) {
				var otherValue = (other as YAPLFloat).FloatValue;
				return new YAPLFloat(FloatValue * otherValue);
			}
			return base.Add(other);
		}

		public override YAPLObject Divide(YAPLObject other) {
			if (other is YAPLInt) {
				var otherValue = (other as YAPLInt).IntValue;
				return new YAPLFloat(FloatValue / otherValue);
			}
			if (other is YAPLFloat) {
				var otherValue = (other as YAPLFloat).FloatValue;
				return new YAPLFloat(FloatValue / otherValue);
			}
			return base.Add(other);
		}

		public override YAPLObject UnaryMinus() {
			return new YAPLFloat(-FloatValue);
		}

		public override YAPLObject UnaryPlus() {
			return new YAPLFloat(FloatValue);
		}

		public override YAPLObject Increment() {
			FloatValue += 1;
			return this;
		}

		public override YAPLObject Decrement() {
			FloatValue -= 1;
			return this;
		}

		#endregion
	}
}
